function* fabonacciSequence() {
  let a = 0;
  let b = 1;
  while (true) {
    [a, b] = [b, a + b];
    yield a;
  }
}

/** 返回第 n 个斐波那契数 */
function fabonacci(n) {
  for (let f of fabonacciSequence()) {
    if (n-- <= 0) return f;
  }
}
console.log(fabonacci(20)); // 10946

function* take(n, iterable) {
  for (const v of iterable) {
    if (n-- > 0) yield v;
    else break;
  }
}
console.log([...take(6, fabonacciSequence())]); // [ 1, 1, 2, 3, 5, 8 ]
